package com.android.infantschool.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\3 0003 13:08
 * @class describe //选项卡显示考勤板块
 */
public class ClockingFragment extends BaseFragment implements View.OnClickListener {

    private RelativeLayout rlTitle;
    private TextView tvTitle;
    private ImageView imgBack;
    private TextView tvSignIn;
    private View viewSignIn;
    private TextView tvClocking;
    private View viewClocking;
    private TextView tvLeave;
    private View viewLeave;

    private ClockingSignInFragment signInFragment;              //签到
    private ClockingClockingFragment clockingFragment;          //考勤
    private ClockingLeaveFragment leaveFragment;                //请假

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_clocking, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rlTitle = view.findViewById(R.id.title);
        tvTitle = view.findViewById(R.id.tv_title);
        imgBack = view.findViewById(R.id.img_back);

        tvTitle.setText("考勤");
        imgBack.setVisibility(View.GONE);
        tvSignIn = (TextView) view.findViewById(R.id.tv_sign_in);
        viewClocking = (View) view.findViewById(R.id.view_clocking);
        tvLeave = (TextView) view.findViewById(R.id.tv_leave);
        viewLeave = (View) view.findViewById(R.id.view_leave);
        viewSignIn = (View) view.findViewById(R.id.view_sign_in);
        tvClocking = (TextView) view.findViewById(R.id.tv_clocking);
        tvClocking.setOnClickListener(this);
        tvSignIn.setOnClickListener(this);
        tvLeave.setOnClickListener(this);
        showTitle(1);

    }

    //进行选中Tab的处理
    private void selectTab(int id) {
        //获取FragmentManager对象
        FragmentManager manager = getChildFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (id) {
            //当选中点击的是微信的Tab时
            case R.id.tv_sign_in://签到
                if (signInFragment == null) {
                    signInFragment = new ClockingSignInFragment();
                    transaction.add(R.id.clocking_fragment, signInFragment);
                } else {
                    //如果对应的Fragment已经实例化，则直接显示出来
                    transaction.show(signInFragment);
                }
                break;
            case R.id.tv_clocking://考勤
                if (clockingFragment == null) {
                    clockingFragment = new ClockingClockingFragment();
                    transaction.add(R.id.clocking_fragment, clockingFragment);
                } else {
                    transaction.show(clockingFragment);
                }
                break;
            case R.id.tv_leave://请假
                if (leaveFragment == null) {
                    leaveFragment = new ClockingLeaveFragment();
                    transaction.add(R.id.clocking_fragment, leaveFragment);
                } else {
                    transaction.show(leaveFragment);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (clockingFragment != null) {
            transaction.hide(clockingFragment);
        }
        if (leaveFragment != null) {
            transaction.hide(leaveFragment);
        }
        if (signInFragment != null) {
            transaction.hide(signInFragment);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sign_in:       //签到
                showTitle(1);
                break;
            case R.id.tv_clocking:      //考勤
                showTitle(2);
                break;
            case R.id.tv_leave:         //请假
                showTitle(3);
                break;
        }
    }

    @SuppressLint("ResourceAsColor")
    private void showTitle(int index) {
        switch (index) {
            case 1:
                tvSignIn.setTextColor(0xFF007C70);
                tvClocking.setTextColor(0xFF000000);
                tvLeave.setTextColor(0xFF000000);
                viewSignIn.setVisibility(View.VISIBLE);
                viewClocking.setVisibility(View.GONE);
                viewLeave.setVisibility(View.GONE);
                selectTab(R.id.tv_sign_in);
                break;
            case 2:
                tvSignIn.setTextColor(0xFF000000);
                tvClocking.setTextColor(0xFF007C70);
                tvLeave.setTextColor(0xFF000000);
                viewSignIn.setVisibility(View.GONE);
                viewClocking.setVisibility(View.VISIBLE);
                viewLeave.setVisibility(View.GONE);
                selectTab(R.id.tv_clocking);
                break;
            case 3:
                tvSignIn.setTextColor(0xFF000000);
                tvClocking.setTextColor(0xFF000000);
                tvLeave.setTextColor(0xFF007C70);
                viewSignIn.setVisibility(View.GONE);
                viewClocking.setVisibility(View.GONE);
                viewLeave.setVisibility(View.VISIBLE);
                selectTab(R.id.tv_leave);
                break;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}